/* GUI CONVERTED from adadcg.p (converter v1.76) Mon Apr  8 13:48:33 2002 */
/* adadcg.p - CHANGE ADDRESS CODE                                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.10.1.10 $                                                     */
/*V8:ConvertMode=Report                                                       */
/* REVISION: 2.0      LAST MODIFIED: 12/18/87   BY: KAT */
/* REVISION: 6.0      LAST MODIFIED: 07/03/90   BY: MLB *D038**/
/* REVISION: 6.0      LAST MODIFIED: 02/26/91   BY: MLB *D379**/
/* REVISION: 6.0      LAST MODIFIED: 04/10/91   BY: WUG *D514**/
/* REVISION: 6.0      LAST MODIFIED: 02/07/92   BY: MLV *F165**/
/* REVISION: 7.0      LAST MODIFIED: 04/15/92   BY: afs *F397**/
/* REVISION: 7.0      LAST MODIFIED: 04/27/92   BY: sas *F439**/
/* REVISION: 7.0      LAST MODIFIED: 05/21/92   BY: afs *F516*   (rev only) */
/* REVISION: 7.3      LAST MODIFIED: 03/25/93   BY: afs *G875**/
/* REVISION: 7.3      LAST MODIFIED: 04/05/93   BY: afs *G904**/
/* REVISION: 7.3      LAST MODIFIED: 05/28/93   BY: mpp *GB39*/
/* REVISION: 7.3      LAST MODIFIED: 06/29/93   BY: cdt *GC79*/
/* REVISION: 7.3      LAST MODIFIED: 07/12/93   BY: cdt *GD34*/
/* REVISION: 7.3      LAST MODIFIED: 08/31/93   BY: cdt *GE65**/
/* REVISION: 7.3      LAST MODIFIED: 01/26/94   BY: dpm *FL63**/
/* REVISION: 7.3      LAST MODIFIED: 06/03/94   BY: dpm *FO59**/
/* REVISION: 7.3      LAST MODIFIED: 09/26/94   BY: jzs *GM54*/
/* REVISION: 7.4      LAST MODIFIED: 02/07/95   BY: jxz *F0HC**/
/* REVISION: 7.4      LAST MODIFIED: 01/12/96   BY: rxm *G0YC**/
/* REVISION: 7.4      LAST MODIFIED: 03/26/96   BY: kjm *G1RG**/
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 09/14/89   BY: *J2ZQ* Poonam Bahl        */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.0      LAST MODIFIED: 03/10/99   BY: *M0B3* Michael Amaladhas  */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* Jacolyn Neder      */
/* REVISION: 9.1      LAST MODIFIED: 02/06/01   BY: *N0VG* Jyoti Thatte       */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Old ECO marker removed, but no ECO header exists *H157*                    */
/* Revision: 1.10.1.9      BY: Nikita Joshi      DATE: 05/31/01  ECO: *M190*  */
/* $Revision: 1.10.1.10 $   BY: Jean Miller       DATE: 04/08/02  ECO: *P056*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* DISPLAY TITLE */

/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdtitle.i "b+ "}
{cxcustom.i "ADADCG.P"}

define new shared variable addr1 like ad_addr label "Old Address".
define new shared variable addr2 like ad_addr label "New Address".
define new shared variable i as integer no-undo.
define new shared variable num_rec as integer no-undo.
define new shared variable cnt_clear like mfc_logical. /* mfcscgb.i */
define new shared variable record_id as recid no-undo.
define new shared variable merge like mfc_logical.
define new shared variable type1 like ad_type.
define new shared variable custend1 like mfc_logical.
define new shared variable adtype like ad_type.
define new shared variable nomaint as character format "x(80)"
   {adlsmt.i}  /* VARIABLE INITIALIZATION */

define variable type2 like ad_type.
define variable l_type1 like ad_type no-undo.
define variable name1 like ad_name.
define variable name2 like ad_name.
define variable yn like mfc_logical.
define variable custend2 like mfc_logical.
define variable site1 like so_site.

define new shared frame b.

define buffer armstr for ar_mstr.
define buffer sobuff for so_mstr.
define buffer pimstr for pi_mstr.

{gpfilev.i}


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
addr1          colon 15
   name1          no-label
   type1          colon 66 skip(1)
   addr2          colon 15
   name2          no-label
   type2          colon 66
with frame a side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

FORM /*GUI*/    /* Standard code change form */
   file_desc      label "Data Set" format "x(30)"
   num_rec        label "Records Updated"
with STREAM-IO /*GUI*/  frame b width 80 down no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame b:handle).

set-a:
repeat:

   display addr1 addr2 with frame a.

   do on error undo, retry with frame a:

      /* BYPASSED THE ENTIRE VALIDATION LOGIC FOR BATCHRUN TO AVOID */
      /* THE BATCHRUN FAILURE IN CASE OF MULTIPLE UPDATE STATEMENTS */
      if not batchrun
      then do:
         set addr1 with frame a
         editing:

            if frame-field = "addr1"
            then do:
               /* FIND NEXT/PREVIOUS RECORD */
               {mfnp.i ad_mstr addr1 ad_addr addr1 ad_addr ad_addr}

               if recno <> ?
               then do:
                  addr1 = ad_addr.
                  display addr1 ad_name @ name1 /* ad_type @ type1 */ .
               end.
            end.
            else do:
               status input.
               readkey.
               apply lastkey.
            end.
         end.

         assign
            name1 = ""
            name2 = ""
            type1 = ""
            type2 = "".

         find ad_mstr
         where ad_addr = addr1 no-lock no-error.
         if not available ad_mstr
         then do:
            {pxmsg.i &MSGNUM=980 &ERRORLEVEL=3}  /* Address does not exist */
            undo, retry.
         end.

     assign
            name1  = ad_name
            adtype = ad_type .

         /* IF ONLY ONE LS_MSTR, FILL TYPE*/
         if type1 = ""
         then do:
            i = 0.
            custend1 = yes.
            for each ls_mstr
            where ls_addr = ad_addr no-lock:
               /* Don't count user-defined ls_types */
               if lookup(ls_type,nomaint) = 0
               then
                  next.
               i = i + 1.
               /* Determine if type is "customer" or "enduser". */
               if (ls_type <> "customer" and ls_type <> "enduser")
               then
                  custend1 = no.

               if i > 1
               and not custend1
               then
                  leave.
               type1 = ls_type.
            end.
            if i > 1
               then type1 = "".
         end.

         /* CHECKING FOR EXISTING ANALYSIS CODE FOR  */
         /* OLD ADDRESS TYPE = CUSTOMER/SHIP-TO      */
         for first ls_mstr
               fields (ls_type ls_addr)  where
               ls_addr = addr1 and
               (ls_type = "customer" or ls_type = "ship-to") no-lock.
         end. /* FOR FIRST LS_MSTR */
         if available ls_mstr
         then do:
            l_type1 = ls_type.
            for first an_mstr
                  fields (an_type an_code) where
                  an_type = "9" and
                  an_code = addr1 no-lock.
            end. /* FOR FIRST AN_MSTR */
            if available an_mstr
            then do:
               /* CUSTOMER ADDRESS MATCHES EXISTING ANALYSIS CODE */
               {pxmsg.i &MSGNUM=2778 &ERRORLEVEL=3 &MSGARG1=an_code}
               undo, retry.
            end.  /* IF AVAILABLE AN_MSTR */
         end. /* IF AVAILABLE LS_MSTR   */

         display addr1 name1 type1 with frame a.
         clear frame b all no-pause.
         hide frame b.
         cnt_clear = yes.

         set2:
         do on error undo:
            set addr2 with frame a.
            merge = no.

            if addr2 = ""
            then do:
               next-prompt addr2 with frame a.
               {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3} /*Blank not allowed */
               undo.
            end.
            if addr2 = addr1
            then do:
               {pxmsg.i &MSGNUM=637 &ERRORLEVEL=3}
               undo, retry.
            end.
            if l_type1 = "customer" or l_type1 = "ship-to"
            then do:
               /* CHECKING FOR EXISTING ANALYSIS CODE FOR NEW ADDRESS */
               for first an_mstr
                     fields (an_type an_code) where
                     an_type = "9" and
                     an_code = addr2 no-lock .
               end. /* FOR FIRST AN_MSTR */
               if available an_mstr
               then do:
                  /* CUSTOMER ADDRESS MATCHES EXISTING ANALYSIS CODE */
                  {pxmsg.i &MSGNUM=2778 &ERRORLEVEL=3 &MSGARG1=an_code}
                  next-prompt addr2 with frame a.
                  undo, retry.
               end.  /* IF AVAILABLE AN_MSTR */
            end. /* IF L_TYPE1 = "CUSTOMER" OR L_TYPE1 = "SHIP-TO" */
         end.
         find ad_mstr
         where ad_addr = addr2 no-lock no-error.
         if available ad_mstr
         then do:
            {pxmsg.i &MSGNUM=630 &ERRORLEVEL=2} /*  EXISTING ADDRESS CODE.*/
            addr2 = ad_addr.
            name2 = ad_name.

            /* IF ONLY ONE LS_MSTR, FILL TYPE*/
            if type2 = ""
            then do:
               i = 0.
               custend2 = yes.
               for each ls_mstr
               where ls_addr = ad_addr no-lock:
                  /* Don't count user-defined ls_types */
                  if lookup(ls_type,nomaint) = 0
                  then
                     next.
                  i = i + 1.
                  /* Determine if type is "customer" or "enduser". */
                  if (ls_type <> "customer" and ls_type <> "enduser")
                  then
                     custend2 = no.

                  if i > 1
                  and not custend1
                  then
                     leave.
                  type2 = ls_type.
               end.
               if i > 1
                  then type2 = "".
            end.
            display addr2 name2 type2 with frame a.

            /* PLEASE CONFIRM MERGE */
             
            {pxmsg.i &MSGNUM=633 &ERRORLEVEL=1 &CONFIRM=merge}
               

            if merge = no
            then do:
               next-prompt addr2.
               undo, retry.
            end.

            /* Allow customer and enduser merge. */
            if not custend1 or not custend2
            then do:
               if type2 <> type1
               then do:
                  {pxmsg.i &MSGNUM=629 &ERRORLEVEL=3}
                  /* ADDRESS TYPES NOT EQUAL, MERGE NOT ALLOWED*/
                  next-prompt addr2.
                  undo, retry.
               end.

               /* DON'T ALLOW MERGE IF CODE USED BY ANOTHER MODULE*/
               find first ls_mstr
               where ls_addr = addr1
                  and ls_type <> type1 /* no-error. */
                  and lookup(ls_type, nomaint) <> 0
                  no-error.
               if available ls_mstr
               then do:
                  {pxmsg.i &MSGNUM=634 &ERRORLEVEL=3}
                  /* CANNOT MERGE - CODE USED BY ANOTHER MODULE */
                  undo, retry.
               end.

               find first ls_mstr
               where ls_addr = addr2
                  and ls_type <> type2 /* no-error. */
                  and lookup(ls_type, nomaint) <> 0
                  no-error.
               if available ls_mstr
               then do:
                  {pxmsg.i &MSGNUM=634 &ERRORLEVEL=3}
                  /* CANNOT MERGE - CODE USED BY ANOTHER MODULE */
                  undo, retry.
               end.
            end.

            /* CHECK FOR ALREADY EXISTING SCHEDULED SALES ORDERS WITH
            THE NEW ADDRESS WHICH HAVE THE SAME SHIP-FROM AD AN ORDER
            FOR THE OLD ADDRESS.  COMPLETION OF THE ADDRESS MERGE WILL
            VIOLATE THE "NO DUPLICATE SHIP-FROM/SHIP-TO COMBINATIONS ON
            SCHEDULED ORDERS" RULE. */

            for each so_mstr no-lock
            where so_cust = addr1 and so_sched
               break by so_site:
               if first-of (so_site)
               then do:
                  site1 = so_site.
                  find first sobuff
                  where sobuff.so_cust = addr2
                    and sobuff.so_site = site1 and sobuff.so_sched
                    and sobuff.so_ship = addr2
                    and sobuff.so_nbr <> so_mstr.so_nbr
                    use-index so_cust no-lock no-error.
                  if available sobuff
                  then do:
                     /* Scheduled SO already exists for addr2 site1 */
                     {pxmsg.i &MSGNUM=1263 &ERRORLEVEL=3
                              &MSGARG1=addr2
                              &MSGARG2=site1}
                     undo set-a, retry.
                  end.
               end.
            end.

            /* DO NOT ALLOW MERGE IF AR APPLICATIONS EXIST WITH SAME CHECK # */
            for each ar_mstr
            where ar_bill = addr2 and
            ar_type       = "P" no-lock:
               find first armstr
               where armstr.ar_check = ar_mstr.ar_check and
               armstr.ar_bill = addr1
               use-index ar_check no-lock no-error.
               if available armstr
               then do:
                  /* Customer checks exist with same check # */
                  {pxmsg.i &MSGNUM=1216 &ERRORLEVEL=3 &MSGARG1=ar_mstr.ar_check}
                  undo set-a, retry.
               end.
            end.

         end.
         display addr2 name2 type2 with frame a.

      end. /* IF NOT BATCHRUN */

      else do:

         update
            addr1
            addr2
            name1
            name2
            type1
            type2 with frame a.

      end. /* IF BATCHRUN */

      find cm_mstr
      where cm_addr = addr1 no-error.

      if not batchrun
      then do:
         yn = yes.
         /* Is all information correct */
         {pxmsg.i &MSGNUM=12 &ERRORLEVEL=1 &CONFIRM=yn}
         if yn = no
         then
            undo, retry.
      end. /* IF NOT BATCHRUN */

      if merge = yes
      then do:

         /* DO NOT ALLOW CUSTOMER MERGE IF PRICE LISTS FOUND WITH */
         /* CUSTOMER/ANALYSIS CODE AS THE ONLY DIFFERENCE         */
         if  type1 = "customer"
         and type2 = "customer"
         then do:
            for each pimstr
               fields(pi_cs_code pi_curr pi_expire pi_list
                      pi_part_code pi_start pi_um)
               where pi_cs_code = addr1
            no-lock:
               if can-find(first pi_mstr
                           where pi_mstr.pi_list      =  pimstr.pi_list
                           and   pi_mstr.pi_cs_code   =  addr2
                           and   pi_mstr.pi_part_code =  pimstr.pi_part_code
                           and   pi_mstr.pi_curr      =  pimstr.pi_curr
                           and   pi_mstr.pi_um        =  pimstr.pi_um
                           and   pi_mstr.pi_start     =  pimstr.pi_start
                           and   pi_mstr.pi_expire    =  pimstr.pi_expire)
               then do:
                  /* PRICE lISTS FOUND FOR ADDRESSES. */
                  /* ADJUST MANUALLY BEFORE MERGE.    */
                  {pxmsg.i &MSGNUM=4659 &ERRORLEVEL=3}
                  undo set-a, retry.
               end. /* IF CAN-FIND(FIRST pi_mstr */
            end. /* FOR EACH pimstr */
         end. /* IF type1 = "customer" */
      end. /* IF merge = yes */
   end.

   bcdparm = "".
   {gprun.i ""gpquote.p"" "(input-output bcdparm,
               6,
               addr1,
               addr2,
               name1,
               name2,
               type1,
               type2,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char,
               null_char)"}

   /* OUTPUT DESTINATION SELECTION */
   {gpselout.i &printType = "printer"
               &printWidth = 80
               &pagedFlag = " "
               &stream = " "
               &appendToFile = " "
               &streamedOutputToTerminal = " "
               &withBatchOption = "yes"
               &displayStatementType = 1
               &withCancelMessage = "yes"
               &pageBottomMargin = 6
               &withEmail = "yes"
               &withWinprint = "yes"
               &defineVariables = "yes"}
/*GUI*/ RECT-FRAME:HEIGHT-PIXELS in frame a = FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.

   {mfphead.i}

   do with frame b:

      view frame b.
      if merge = yes
      then do:
           
                for each cm_ where cm_addr=addr1:
                   delete cm_.
                end.
                
                for each ad_ where ad_addr= addr1:
                  ad_type='ship-to'.
                  ad_ref =addr2.
                end.
                
                FOR EACH ls_ WHERE ls_addr = addr1:
                    ls_type='ship=to' .
                END.
            
         {gprun.i ""adadcgf.p""}

      end.
      else do:   /*merge = no*/
         {gprun.i ""adadcgc.p""}

      end.

      /*FOR BOTH MERGE AND CHANGE DO THE FOLLOWING*/
      {gprun.i  ""adadcga.p""}
      {gprun.i  ""adadcgb.p""}
       
       FOR EACH so_ WHERE so_cust =addr2:
           so_ship = addr1.
       END.
      /* CHANGING OLD CUSTOMER PRICE LIST WITH NEW CUSTOMER */
      /* FOR BOTH MERGE AND CHANGE                          */
      if l_type1 = "customer" or l_type1 = "ship-to"
      then do:
         num_rec = 0.
         for each pi_mstr exclusive-lock
               where pi_cs_code = addr1:

            assign
               pi_cs_code = addr2
               num_rec = num_rec + 1.
            release pi_mstr.
         end. /* FOR EACH PI_MSTR */
      end. /* IF L_TYPE1 = CUSTOMER OR L_TYPE1 = SHIP-TO */
      {mfcscgb.i pi_mstr}   /* DISPLAY STATUS IN FRAME b */

      /* change customer numbers in ar_mstr.ar_nbr FOR PAYMENT TRANSACTIONS */

      {gprun.i  ""adadcge.p""}
      {&ADADCG-P-TAG1}

      hide message no-pause.
      {pxmsg.i &MSGNUM=632 &ERRORLEVEL=1}
      /*Change address code process completed */

   end.

   {mfrtrail.i}
end.
